import util.GuiImg;
import util.FileSerialization;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Atm extends JFrame implements ActionListener {
    CardLayout cardLayout=new CardLayout();
    private Image atmImg=GuiImg.getImage("/imgs/background.jpg");
    private ArrayList<User> userArray=new ArrayList();
    private Check check=new Check(this);
    private Login login=new Login(this);
    private Menu menu=new Menu(this);
    private Deposit deposit=new Deposit(this);
    private Get get=new Get(this);
    private Alter alter=new Alter(this);
    private Transfer transfer=new Transfer(this);
    private  static Atm atm=new Atm();
    private int index;

    public Atm(){
        this.setContentPane(new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(atmImg,0,0,800,600,this);
            }
        });
        this.setLayout(cardLayout);
        //登录
        this.add("登录",login);
        //菜单
        this.add("菜单",menu);
        //存款
        this.add("存款",deposit);
        //取款
        this.add("取款",get);
        //查询
        this.add("查询",check);
        //改密
        this.add("改密",alter);
        //转账
        this.add("转账",transfer);

        //界面初始化
        this.setSize(800,600);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {

    }

    public static void main(String[] args) {
        atm.init();
        System.out.println(atm.getUserArray());
    }
    public void switchPanel(String name){
        cardLayout.show(this.getContentPane(),name);
    }
    public void init(){
        getConnection();
        ArrayList userarray= (ArrayList) query();
        if (userarray.size()==0){
            insert("aaaaaa","111111",10000);
            insert("bbbbbb","222222",20000);
            insert("cccccc","333333",30000);
            insert("dddddd","444444",40000);
            insert("eeeeee","555555",50000);
        }
    }
    public void itin(){
        new FileSerialization();
        FileSerialization.fileserialization("D:/a.txt",atm.getUserArray());
    }
    public ArrayList<User> getUserArray() {
        return userArray;
    }

    public void setUserArray(ArrayList<User> userArray) {
        this.userArray = userArray;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Check getCheck() {
        return check;
    }

    public void setCheck(Check check) {
        this.check = check;
    }

    public Deposit getDeposit() {
        return deposit;
    }

    public void setDeposit(Deposit deposit) {
        this.deposit = deposit;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    //数据库连接
    public Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/z725?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "z6787526");
        } catch (ClassNotFoundException | SQLSyntaxErrorException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //添加数据
    public void insert(String id,String password,double money) {
        //1.创建连接
        Connection con = getConnection();
        Statement statement = null;
        try {
            //2.获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "insert into t_user(a_id,a_password,a_money) values ('"+id+"','"+password+"',"+money+")";
            //3.执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响的行数:" + row);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //查询数据
    public java.util.ArrayList query() {
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        ArrayList userList=new ArrayList();
        try {
            //2.获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "select * from t_user";
            //3.执行SQL语句
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                User user=new User();
                user.setId(rs.getString(1));
                user.setPassword(rs.getString(2));
                user.setMoney(rs.getDouble(3));
                atm.getUserArray().add(user);
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return userList;
    }

    //改
    public void update(String password,String ID) {
        Connection con = getConnection();
        Statement statement = null;
        try {
            //2.获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "update t_user set a_password='"+password+"' where a_id='"+ID+"'";
            //3.执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响的行数:" + row);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //存取
    public void deposit(Double money,String ID) {
        Connection con = getConnection();
        Statement statement = null;
        try {
            //2.获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "update t_user set a_money='"+money+"' where a_id='"+ID+"'";
            //3.执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响的行数:" + row);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //转账
    public void transfer(Double money,Double money1,String ID,String ID1) {
        Connection con = getConnection();
        Statement statement = null;
        try {
            //2.获取执行SQL语句对象
            statement = con.createStatement();
            String sql = "update t_user set a_money='"+money+"' where a_id='"+ID+"'";
            String sql1 = "update t_user set a_money='"+money1+"' where a_id='"+ID1+"'";
            //3.执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响的行数:" + row);
            int row1 = statement.executeUpdate(sql1);
            System.out.println("影响的行数:" + row1);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
